package com.soho.helper;

import java.time.LocalDateTime;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

import com.jfinal.plugin.activerecord.Record;
import com.soho.controller.ApiWebsocket;
import com.soho.core.Mt4;

import online.mtapi.mt4.QuoteEventArgs;
import online.mtapi.mt4.QuoteEventHandler;
import online.mtapi.mt4.Timeframe;

public class QuoteHelper {
	
	private static Map<String,Record> cacheM1 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheM5 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheM15 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheM30 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheH1 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheH4 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheD1 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheW 	= new ConcurrentHashMap<String, Record>();
	private static Map<String,Record> cacheMN 	= new ConcurrentHashMap<String, Record>();
	
	//更新几个表吧
	public static void updateQuote(QuoteEventArgs args) {
		LocalDateTime fromTime	=	args.Time;
		String symbol			=	args.Symbol;
		
		String timeM1			=	StatusHelper.getM1(fromTime);
		String timeM5			=	StatusHelper.getM5(fromTime);
		String timeM15			=	StatusHelper.getM15(fromTime);
		String timeM30			=	StatusHelper.getM30(fromTime);
		String timeH1			=	StatusHelper.getH1(fromTime);
		String timeH4			=	StatusHelper.getH4(fromTime);
		String timeD1			=	StatusHelper.getD1(fromTime);
		String timeW			=	StatusHelper.getW(fromTime,symbol);
		String timeMN			=	StatusHelper.getMN(fromTime);
		
		updateCommon(args,ContentHelper.TABLE_QUOTES_M1,cacheM1,timeM1,Timeframe.M1.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_M5,cacheM5,timeM5,Timeframe.M5.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_M15,cacheM15,timeM15,Timeframe.M15.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_M30,cacheM30,timeM30,Timeframe.M30.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_H1,cacheH1,timeH1,Timeframe.H1.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_H4,cacheH4,timeH4,Timeframe.H4.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_D1,cacheD1,timeD1,Timeframe.D1.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_W,cacheW,timeW,Timeframe.W1.getValue());
		updateCommon(args,ContentHelper.TABLE_QUOTES_MN,cacheMN,timeMN,Timeframe.MN1.getValue());
	}
	
	private static void updateCommon(QuoteEventArgs bar,String table,Map<String,Record> cacheMap,String cacheKey,int timeFrameValue) {
		String symbol			=	bar.Symbol;
		double bid 				=	bar.Bid;
		Record record			=	null;
		if(cacheMap.containsKey(cacheKey)) {
			record				=	cacheMap.get(cacheKey);
		}else {
			
			String preTime		=	StatusHelper.getPreTime(cacheKey, timeFrameValue);
			
			//获取最后一条,进行更新，
			if(cacheMap.containsKey(preTime)) {
				SqlHelper.updateRecord(table, cacheMap.get(preTime));
			}
			//释放内存	
			cacheMap.clear();
			
			record		=	SqlHelper.getRecord(table, symbol, cacheKey);
			if(record 	==	null) {
				SqlHelper.saveRecord(table, new Record().set("Close", bid).set("High", bid).set("Low",bid).set("Open", bid).set("Symbol", symbol).set("Time", cacheKey));
			}
			cacheMap.put(cacheKey, record);
		}
		
		double low				=	record.getDouble("Low");
		double high				=	record.getDouble("High");
		boolean boolChange		=	false;
		//大于最高价,则更新
		if(bid>high) {
			record.set("High", bid);
			boolChange			=	true;
		}else if(bid<low) {
			record.set("Low", bid);
			boolChange			=	true;
		}

		record.set("Close", bid);
		if(boolChange) {
			cacheMap.put(cacheKey, record);
			SqlHelper.updateRecord(table, record);
		}
	}
	
	public static void startListen() {
		//数据推送
		Mt4.getInstance().getClient().OnQuote.addListener(new QuoteEventHandler() {
			@Override
			public void invoke(Object sender, QuoteEventArgs args) {
				ApiWebsocket.sendMsg(args);
				//实时计算
				QuoteHelper.updateQuote(args);
			}
		});
	}
	
}
